package nku.csc360;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class homework8 extends JFrame{
	public homework8(){
		add(new NewPanel());
	}
  
    public static void main(String[] args) {
    	
    	//Setting up Frame
    	homework8 frame = new homework8();
    	frame.setSize(800, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

	
    }

}

@SuppressWarnings("serial")
class NewPanel extends JPanel  implements ItemListener{
	
	private int radioOption;			//0 FOR LINE , 1 FOR RECTANGLE, 2 FOR OVAL
	private boolean checkOption;		//TRUE FOR FILLED, FALSE FOR UNFILLED
	private JRadioButton line = new JRadioButton("Line", true);
	private JRadioButton rectangle = new JRadioButton("Rectangle");
	private JRadioButton oval = new JRadioButton("Oval");
	private JRadioButton triangle = new JRadioButton("Triangle");
  	private JCheckBox filled  = new JCheckBox("Filled",true);
  	private ButtonGroup group = new ButtonGroup(); // Had to group them otherwise can click multiple radio buttons
  	
 	NewPanel(){
 		//Adding to group because otherwise could click multiple radio buttons
		group.add(line);
		group.add(rectangle);
		group.add(oval);
		group.add(triangle);
		//this.add(group); 
		
		//Adding each option
		this.add(line);
		this.add(rectangle);
		this.add(oval);
		this.add(triangle);
		this.add(filled);
		
		//Setting Listener Events
		line.addItemListener(this);
  		rectangle.addItemListener(this);
    	oval.addItemListener(this);
    	triangle.addItemListener(this);
    	filled.addItemListener(this);
 	}
 	
	//Setting what happens when listener events are envoked
 	public void itemStateChanged(ItemEvent e) {
   	
   	 	if (line.isSelected()) 
   	 	{
   	 		setRadioOption(0);
   	 		filled.setVisible(true);
   	 	}   
   	 	else if (rectangle.isSelected())
   	 	{
   	 		setRadioOption(1);
   	 		filled.setVisible(true);
   	 	}
  	 	else if (oval.isSelected())
  	 	{ 
  	 		setRadioOption(2);
  	 		filled.setVisible(true);
  	 	}
  	 	else if (triangle.isSelected()){
  	 		setRadioOption(3);
  	 		filled.setVisible(false);
  	 		setCheckOption(false);
  	 		//filled.setEnabled(false)
  	 		
  	 	}
  	 
  	 	if (filled.isSelected()) 
  	 	{
  	 		setCheckOption(true);
  	 	} 
  	 	else
  	 	{
  	 		setCheckOption(false);
  	 	}
  	 
  }
  
    // Setter for radio button
	public void setRadioOption(int value) {
    	radioOption = value;
    	repaint();
  	}

	// Setter for check button
	public void setCheckOption(boolean value) {
    	checkOption = value;
    	repaint();
  	}
  	
  	public int getRadioOption(){
  		return radioOption;
  	}
  	
  	public boolean getCheckOption(){
  		return checkOption;
  	}
  	
  	
 	public void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	int startWidth = getWidth()/5;
    	int startHeight = getHeight()/5;
    	int endWidth = getWidth()*3/5;
		int endHeight = getHeight()*3/5;
		int distance;
		if (endWidth < endHeight)
			distance = endWidth;
		else 
			distance = endHeight;
		
		int x[] = {startWidth,startWidth+distance,(2*startWidth + distance)/2 };
		int y[] = {startHeight,startHeight, startHeight+distance};		
			
		if(radioOption == 0){
   			g.drawLine(startWidth, startHeight, endWidth+startWidth, endHeight+startHeight);
		}
   		else if(radioOption == 1){
	   		if (checkOption == true)
	      		  g.fillRect(startWidth, startHeight, endWidth, endHeight);
			else
	       		 g.drawRect(startWidth, startHeight, endWidth, endHeight);
   		}
   		else if(radioOption == 2){
      		if (checkOption == true)
	    	    g.fillOval(startWidth, startHeight, endWidth, endHeight);
	   		else
    	    	g.drawOval(startWidth, startHeight, endWidth, endHeight);
    	}
    	else if(radioOption == 3){
    	//	if (checkOption == true){
    	//		g.fillPolygon(x,y,3);
    	//	}
    	//	else
    			g.drawPolygon(x,y,3);
    	}
  }
}
